// ============================== 导入依赖 ============================== //

import CORE from '@/CORE'
import Client from '@api/order/api/WorkOrderApi'

import OneClass from '@api/order/model/QueryWorkOrderDetailReq'
import EditClass from '@api/order/model/UpdateWorkOrderReq'
import DelClass from '@api/order/model/DeleteWorkOrderReq'
import BaseClass from '@api/order/model/WorkOrderDto'
import FileClass from '@api/order/model/WorkOrderFileDto'

const CLIENT = CORE.grpcCLIENT(Client)
const FILE = [FileClass, 'workOrderFileDto', 1]

// ============================== 导出远程接口 ============================== //

export default {
  // —————————————————————————————— 单项 —————————————————————————————— //

  validOne(one) {
    return one.id
  },
  async getOne({ id }, TIP = '查询详情') {
    const DTO = CORE.grpcDTO(OneClass, { id })
    const resp = await CORE.grpc(TIP, CLIENT, 'queryWorkOrderDetail', DTO)
    return resp.workOrderDto || {}
  },
  transOne(obj) {
    const fileList = obj.workOrderFileDto || []
    obj.imageList = fileList.filter((item) => item.fileType === 1)
    obj.audioList = fileList.filter((item) => item.fileType === 2)

    const backup = obj.workOrderPreAssetDto || []
    backup.forEach((item, i) => {
      item.index = i + 1
      item.unitPrice = +(item.unitPrice / 100).toFixed(2)
      item.totalPrice = +(item.totalPrice / 100).toFixed(2)
    })

    return obj
  },

  // —————————————————————————————— 改 —————————————————————————————— //

  transEdit(one) {
    const status = one.resolverUserId ? 'task_receive' : 'task_assign'
    return { ...one, status }
  },
  async edit(one, TIP = '修改') {
    const BASE = [BaseClass, 'workOrderDto', '', (dto, obj) => CORE.grpcDTO(dto, obj, FILE)]
    const DTO = CORE.grpcDTO(EditClass, one, BASE)
    await CORE.grpc(TIP, CLIENT, 'updateWorkOrder', DTO)
    return CORE.grpcOK(TIP)
  },

  // —————————————————————————————— 删 —————————————————————————————— //

  async del({ id }, TIP = '删除') {
    const DTO = CORE.grpcDTO(DelClass, { ids: id })
    await CORE.grpc(TIP, CLIENT, 'deleteWorkOrder', DTO)
    return CORE.grpcOK(TIP)
  },
}
